$(document).ready(function(e){
	createPageTab();
	createEmployeeTab();
	setPosition("positionSelect");
	$("#positionSelect").val($("#positionInput").val());
	findList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var list = null;
var curPageNum = null;
function findList(pageNow){
	var tmp2=root+"/admin/employee!findList";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "position.id":$("#positionSelect").val(), "employee.userName": $("#employeePid").val(), "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			if(list!=null) createEmployeeTab();
			totalPageNum = json.totalPageNum;
			list = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].userName+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].position.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				if(json.pageData[i].employee!=null){
					htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].employee.userName+'</div></td>';
					htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				}else{
					htmlStr += '<div align="center" class="STYLE2 STYLE1"></div></td>';
					htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				}
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdate('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="del('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findList(json.totalPageNum);});
		}
	});
}

function setPosition(divId){
	var tmp=root+"/admin/position!getAll";
	$.ajax({
		url: tmp,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.length; i++){
				htmlStr += '<option value="'+json[i].id+'">'+json[i].name+'</option>';
			}
			if($("#"+divId).html()=='<option value="">--</option>'){
				$("#"+divId).append(htmlStr);
			}else{
				$("#"+divId).html(htmlStr);
			}
		}
	});
}

function setEmployees(divId){
	var tmp=root+"/admin/employee!getAll";
	$.ajax({
		url: tmp,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.length; i++){
				htmlStr += '<option value="'+json[i].id+'">'+json[i].userName+' ('+json[i].position.name+')'+'</option>';
			}
			if($("#"+divId).html()=='<option value="">--</option>'){
				$("#"+divId).append(htmlStr);
			}else{
				$("#"+divId).html(htmlStr);
			}
		}
	});
}

function showAdd(){
	setPosition("position_add");
	setEmployees("employee_add");
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAdd(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function add(){
	if($("#userName_add").val()=="" || $("#userName_add").val()==null){
		alert("用户名不能为空！");
		return;
	}
	if($("#passWord_add").val()=="" || $("#passWordTwo_add").val()==""){
		alert("密码不能为空！");
		return;
	}
	if($("#passWordTwo_add").val()!=$("#passWord_add").val()){
		alert("重复密码必须与密码相同！");
		return;
	}
	$("#addForm").submit();
}

function del(id){
	var ids = "";
	if(id==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个业务员！");
			return;
		}
	}
	
	var tmp2=root+"/admin/employee!delete";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"employee.id":id, "ids":ids, "page.curPageNum":curPageNum, "isDesc": isDesc, "position.id":$("#positionSelect").val(), "employee.userName": $("#employeePid").val()},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findList(json.curPageNum);
		}
	});
}

function showUpdate(pageNow, index){
	setPosition("position_u");
	setEmployees("employee_u");
	
	$("#position_u").val(list[index].position.id);
	if(list[index].employee!=null)$("#employee_u").val(list[index].employee.id);
	
	$("#employeeId_u").val(list[index].id);
	$("#userName_u").val(list[index].userName);
	
	$("#backGroud").show();
	$("#updateDiv").show();
	
	$("#updateButton").bind("click",function(){update(pageNow, index);});
}

function closeUpdate(){
	$("#updateForm")[0].reset();
	$("#employee_u").html('<option value="">--</option>');
	$("#updateButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function isUpdatePwd(){
	if($("#pwdCheckBox").attr("checked")=="checked"){
		$("#old_pwd_u_tr").show();
		$("#new_pwd_u_tr").show();
		$("#new_pwdTwo_u_tr").show();
	}else{
		$("#old_pwd_u_tr").hide();
		$("#new_pwd_u_tr").hide();
		$("#new_pwdTwo_u_tr").hide();
	}
}

function update(pageNow, index){
	var isUpdatePwdStr = 0;
	if($("#pwdCheckBox").attr("checked")=="checked"){
		if(!v_oldPwd(list[index].id, $("#old_pwd_u").val())){
			alert("请输入正确的旧密码！");
			return;
		}
		if($("#new_pwd_u").val()=="" || $("#new_pwdTwo_u").val()==""){
			alert("密码不能为空！");
			return;
		}
		if($("#new_pwd_u").val()!=$("#new_pwdTwo_u").val()){
			alert("重复密码必须与密码相同！");
			return;
		}
		isUpdatePwdStr = 1;
	}
	var tmp2=root+"/admin/employee!update";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"employee.id":$("#employeeId_u").val(), "isUpdatePwd": isUpdatePwdStr, "employee.password":$("#new_pwd_u").val(),
				"position.id":$("#position_u").val(), "company.id":$("#employee_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdate();
			if(json.code=="n"){
				alert("修改失败！");
			}else{
				findList(json.curPageNum);
			}
		}
	});
}

function v_oldPwd(id, pwd){
	var a = false;
	var tmp2=root+"/admin/employee!verificationPwd";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"employee.id":id, "employee.password":pwd},
		dataType:"json",
		success: function(json){
			if(json.code=="y"){
				a = true;
			}
		}
	});
	return a;
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findList(p);
	}
}